<?php
    /**
     * Created by PhpStorm.
     * User: zgh
     * Date: 2021-08-05
     * Time: 14:41
     */

    namespace app\admin\controller;


    use app\admin\service\AdvService;
    use app\common\Annotation\ControllerAnnotation;
    use app\common\Annotation\NodeAnotation;
    use app\common\controller\BackendController;
    use app\common\exception\ServiceException;
    use think\facade\Request;


    /**
     * @ControllerAnnotation(title="广告管理",auth=2)
     * Class AdminController
     * @package app\admin\controller
     */
    class AdvController extends BackendController
    {

        /**
         * @NodeAnotation(title="列表",auth=2)
         * @return mixed|\think\response\Json
         */
        public function index()
        {
            if (Request::isAjax()) {
                $result = AdvService::getList(Request::param());
                return success_json_response("成功", $result['dataList'], ['count' => $result['total']]);
            } else {
                return $this->fetch();
            }
        }

        /**
         * @NodeAnotation(title="添加",auth=2)
         * @return mixed|\think\response\Json
         * @throws ServiceException
         */
        public function add()
        {
            $params = Request::param();
            if (Request::isAjax()) {
                $res = AdvService::save($params);
                if ($res) {
                    return success_json_response("添加成功", ["id" => $res]);
                } else {
                    return json_response("添加失败");
                }
            }
            return $this->fetch();
        }

        /**
         * @NodeAnotation(title="修改",auth=2)
         * @return mixed|\think\response\Json
         * @throws ServiceException
         */
        public function edit()
        {
            $params = Request::param();
            if (Request::isAjax()) {
                $res = AdvService::save($params);
                return $res ? success_json_response("修改成功", ["id" => $res]) : json_response("修改失败");
            }
            $model = AdvService::getByID($params['id']);
            $this->assign("model", $model);
            return $this->fetch();
        }

        /**
         * @NodeAnotation(title="删除",auth=2)
         * @return \think\response\Json
         */
        public function del()
        {
            if (Request::isAjax()) {
                $res = AdvService::del(Request::param());
                return $res ? success_json_response("删除成功") : json_response("删除失败");
            }
        }
    }